package com.view.tree;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import com.view.MainView;

public class CheckBoxTreeNodeSelectionListener extends MouseAdapter {

	private MainView mainView;
	private JTree tree;
	private JPopupMenu menu;

	private TreeNode root;
	private CheckBoxTreeNode currentNode;
	private Point point;

	public CheckBoxTreeNodeSelectionListener(MainView mainView, JTree tree) {
		super();
		this.mainView = mainView;
		this.tree = tree;
		this.root = (TreeNode) tree.getModel().getRoot();
		JMenuItem mexpand, mclose, mlook, mrun, mselect, munselect;
		menu = new JPopupMenu();
		mexpand = new JMenuItem("全部展开");
		menu.add(mexpand);
		mclose = new JMenuItem("全部关闭");
		menu.add(mclose);
		mlook = new JMenuItem("查看");
		menu.add(mlook);
		mrun = new JMenuItem("执行选中");
		menu.add(mrun);
		mselect = new JMenuItem("全选");
		menu.add(mselect);
		munselect = new JMenuItem("取消全选");
		menu.add(munselect);

		mexpand.addActionListener(e -> expandAll(tree, new TreePath(root), true));
		mclose.addActionListener(e -> expandAll(tree, new TreePath(root), false));
		mlook.addActionListener(e -> {
			if (currentNode != null)
				JOptionPane.showMessageDialog(mainView, currentNode.name);
		});
		// 全选节点
		mselect.addActionListener(e -> selectAllNode());
		// 取消全选节点
		munselect.addActionListener(e -> unSelectAllNode());
		//执行选中节点
		mrun.addActionListener(e -> getSelectNode(((CheckBoxTreeNode) root)));
	}

	// 全选节点
	public void selectAllNode() {
		((CheckBoxTreeNode) root).setSelected(true);
		((DefaultTreeModel) tree.getModel()).nodeChanged(((CheckBoxTreeNode) root));
		// 该方法会自动折叠节点
		// ((DefaultTreeModel) tree.getModel()).nodeStructureChanged(((CheckBoxTreeNode)
		// root));
	}

	public void getSelectNode(CheckBoxTreeNode node) {
		Queue<CheckBoxTreeNode> queue = new LinkedList<>();
		Enumeration<?> chidrens = node.children();
		while (chidrens.hasMoreElements()) {
			CheckBoxTreeNode tmpNode = (CheckBoxTreeNode) chidrens.nextElement();
			if (tmpNode.children().hasMoreElements()) {
				queue.offer(tmpNode);
			} else {
				mainView.addLog(tmpNode.name);
			}
		}
		while (!queue.isEmpty()) {
			CheckBoxTreeNode tmpNode = queue.poll();
			Enumeration<?> tmpNodeChildren = tmpNode.children();
			while (tmpNodeChildren.hasMoreElements()) {
				CheckBoxTreeNode curNode = (CheckBoxTreeNode) tmpNodeChildren.nextElement();
				if (curNode.children().hasMoreElements()) {
					queue.offer(curNode);
				} else {
					mainView.addLog(curNode.name);
				}
			}
		}

	}

	// 取消全选节点
	public void unSelectAllNode() {
		((CheckBoxTreeNode) root).setSelected(false);
		((DefaultTreeModel) tree.getModel()).nodeChanged(((CheckBoxTreeNode) root));
		// 该方法会自动折叠节点
		// ((DefaultTreeModel) tree.getModel()).nodeStructureChanged(((CheckBoxTreeNode)
		// root));
	}

	public List<String> getSelectedNodeId() {
		return null;
	}

	// 展开树的所有节点的方法
	public void expandAll(boolean expand) {
		expandAll(tree, new TreePath(root), expand);
	}

	// 展开树的所有节点的方法
	public void expandAll(JTree tree, TreePath parent, boolean expand) {
		TreeNode node = (TreeNode) parent.getLastPathComponent();
		if (node.getChildCount() >= 0) {
			for (Enumeration<?> e = node.children(); e.hasMoreElements();) {
				TreeNode n = (TreeNode) e.nextElement();
				TreePath path = parent.pathByAddingChild(n);
				expandAll(tree, path, expand);
			}
		}
		if (expand) {
			tree.expandPath(parent);
		} else {
			tree.collapsePath(parent);
		}
	}

	@Override
	public void mouseClicked(MouseEvent event) {
		if (SwingUtilities.isRightMouseButton(event)) {
			// 弹出右键菜单
			currentNode = null;
			point = event.getPoint();
			int row = tree.getRowForLocation(point.x, point.y);
			TreePath path = tree.getPathForRow(row);
			if (path != null) {
				CheckBoxTreeNode node = (CheckBoxTreeNode) path.getLastPathComponent();
				if (node != null)
					currentNode = node;
			}
			menu.show(mainView, point.x, point.y);
			return;
		}

		point = event.getPoint();
		int row = tree.getRowForLocation(point.x, point.y);
		TreePath path = tree.getPathForRow(row);
		if (path != null) {
			CheckBoxTreeNode node = (CheckBoxTreeNode) path.getLastPathComponent();
			if (node != null) {
				boolean isSelected = !node.isSelected();
				node.setSelected(isSelected);
				//((DefaultTreeModel) tree.getModel()).nodeChanged(node);
				// 该方法会自动折叠节点
				((DefaultTreeModel) tree.getModel()).nodeStructureChanged(node);
			}
		}
	}

}
